Skip to content

Taxonomy: Add a pre_update_term filter to wp_update_term() - #16299

Open
faisalahammad wants to merge 1 commit into
WordPress:trunkfrom
faisalahammad:ticket/58404-pre-update-term
Open

faisalahammad wants to merge 1 commit into
WordPress:trunkfrom
faisalahammad:ticket/58404-pre-update-term

Conversation

@faisalahammad

Copy link
Copy Markdown

Adds a pre_update_term filter to wp_update_term(), modeled on pre_insert_term in wp_insert_term(), so a callback can reject a term update with a WP_Error before anything is written.

Trac ticket: https://core.trac.wordpress.org/ticket/58404

Description

The existing wp_update_term_data filter can change the data used for the update but cannot abort it, and returning a WP_Error from it is ignored. This patch adds a pre_update_term filter as a gate, mirroring how pre_insert_term works in wp_insert_term().

Details, addressing the discussion on the ticket:

  • The filter only fires when the term exists. It runs after the term has been resolved and after the empty-term check, so missing terms keep returning their own errors and the filter never receives a WP_Error (feedback from azaozz in comment 5).
  • Returning a WP_Error from the filter short-circuits the update and is returned as-is. This is the use case from the ticket and closes the gap confirmed in comment 21.
  • Returning any other value that is not a WP_Term object short-circuits the update with an invalid_term error, so an unexpected return value cannot reach the code that follows. This is the safeguard discussed in comments 19 and 21.
  • Changing the term data before the update remains the job of the existing wp_update_term_data filter. The new filter is only a gate.

One observation for discussion: the alias_of handling in both wp_insert_term() and wp_update_term() calls wp_update_term() internally to keep the term group in sync and discards the return value, so a veto from the filter during that internal call would be silent. That is a pre-existing pattern that also applies to wp_update_term_data, and handling it would change the error contracts of both functions, so it is left out of this patch.

This supersedes #4528 and includes the feedback raised on the ticket there.

Testing Instructions

Run the new unit tests:

npm run test:php -- --filter Tests_Term_WpUpdateTerm

To test manually, add the following and then edit a term in wp-admin:

add_filter( 'pre_update_term', function( $term, $taxonomy, $args ) {
	if ( 'locked' === $term->slug ) {
		return new WP_Error( 'term_locked', __( 'This term cannot be updated.' ) );
	}
	return $term;
}, 10, 3 );

Updates to a term with the slug locked are rejected with the error, and edit_terms never fires for it. Other terms update normally.

Use of AI Tools

AI assistance: Yes
Tool(s): ZCode
Model(s): GLM 5.3
Used for: Ticket research, the patch and unit tests were drafted with AI assistance; the implementation, tests, and coding standards were reviewed and verified by me.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Mirrors pre_insert_term in wp_insert_term(). Returning a WP_Error
object short-circuits the update so callbacks can validate a term
and reject the change before anything is written.

Returning a value that is not a WP_Term object short-circuits the
update with an invalid_term error to guard the code that follows
against an unexpected filter return value.

The filter only fires when the term exists, so missing terms keep
returning their own errors untouched.

Props kobi1, nihar007, 2mkabir, oglekler, azaozz, joemcgill, hellofromTonya, ankitp47.

Fixes #58404.
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props faisalahammad.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant